class ContactsController < ApplicationController
  before_filter :require_admin, :only => [:edit, :update, :destroy, :admin, :show]
  before_filter :init, :only => [:new, :create]
  def new
    @page["styles"] << "roundmenuUI"
    @page["meta"] = false
    @page["name"] = "contact"
    @qas = Qa.find(:all, :conditions => ["status = ? and position = ?", "approved", "contact"])
    @contact = Contact.new
  end

  def create
    @contact = Contact.new params[:contact]
    @page["meta"] = false
    #if verify_recaptcha && @contact.save TODO: uncomment
    if @contact.save
      @page["name"] = "contact.thanks"
    else
      @page["styles"] << "roundmenuUI"
      @page["name"] = "contact"
      render :action => "new"
    end
  end

  def edit
    set_form_settings
    @page["name"] = "contact"
    @page["not_show"] = "update"
    @contact = Contact.find(:first, :conditions => ["id = ?", params[:id]])
    render :layout => "form"
  end

  def update
    @contact = Contact.find(:first, :conditions => ["id = ?", params[:id]])
    if @contact.update_attributes(params[:contact])
      redirect_to contact_admin_path and return
    else
      set_form_settings
      @page["name"] = "contact"
      @page["not_show"] = "update"
      render :action => "edit", :layout => "form"
    end
  end

  def destroy
    @contact = Contact.find params[:id]
    @contact.destroy
    redirect_to contact_admin_path
  end

  def admin
    per_page = params[:per_page] ||= @site["per_page"]["admin"]["contacts"]
    @contacts = Contact.find(:all).uniq.paginate(:page => params[:page], :per_page => per_page)
    set_admin_settings
    render :layout => "cms"
  end

end
